﻿-- =============================================
-- Author:		Maxime Forget-Séguin
-- Create date: 28 février 2008
-- Description:	Indique si la valeur d'une propriété est unique
-- =============================================
ALTER PROCEDURE [dbo].[medianamik_PropertyValueIsUnique]
	-- Add the parameters for the stored procedure here
	@NodeID uniqueidentifier,
	@PropertyID uniqueidentifier, 
	@Value varchar(max)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT CASE WHEN COUNT(medianamik_Property.PropertyId) > 0 THEN 'false' ELSE 'true' END FROM medianamik_NodePropertyValue INNER JOIN
	(SELECT NodeId, PropertyId, MAX(ISNULL(Revision, dbo.fn_medianamik_MaxRevision())) AS Revision
		FROM medianamik_NodePropertyValue 
		where PropertyId = @PropertyID
		GROUP BY PropertyId, NodeId) as Revisions
		ON medianamik_NodePropertyValue.NodeId = Revisions.NodeId and
		medianamik_NodePropertyValue.PropertyId = Revisions.PropertyId and medianamik_NodePropertyValue.Revision = Revisions.Revision
	INNER JOIN medianamik_Property
	ON medianamik_NodePropertyValue.PropertyId = medianamik_Property.PropertyId
	INNER JOIN medianamik_Node
	ON medianamik_Node.NodeID = medianamik_NodePropertyValue.NodeID
	WHERE medianamik_NodePropertyValue.NodeId <> @NodeID
	AND medianamik_NodePropertyValue.PropertyId = @PropertyID
	AND medianamik_NodePropertyValue.[Value] = @Value
	AND medianamik_Node.IsDeleted = 0
END








